﻿/************************************************
 * Author       :   XXY
 * Blog         :   https://www.cnblogs.com/jzyl
 * Gitee        :   https://gitee.com/xiaobaichong
 * Email        :   1047185209@QQ.com
 * FileName     :   ButtonToggle.cs
 * CreateData   :   2023/11/11 13:41:08
 * UnityVersion :   2021.3.30f1c1
 * Description  :   按钮开关（切换状态）
************************************************/

using UnityEngine;
using Sirenix.OdinInspector;
using UnityEngine.UI;
using UnityEngine.Events;


[RequireComponent(typeof(Button))]
public class ButtonToggle : MonoBehaviour
{
    [OnValueChanged("ChangeSprite")]
    [SerializeField] bool isOn;
    [SerializeField] [HorizontalGroup("Image")] [LabelWidth(75)] Image Img;
    [SerializeField] [HorizontalGroup("Image")] [LabelWidth(75)] Sprite onSprite;
    [SerializeField] [HorizontalGroup("Image")] [LabelWidth(75)] Sprite offSprite;

    [SerializeField] [HorizontalGroup("Text")] [LabelWidth(75)] Text Txt;
    [SerializeField] [HorizontalGroup("Text")] [LabelWidth(75)] string onText;
    [SerializeField] [HorizontalGroup("Text")] [LabelWidth(75)] string offText;

    [SerializeField] [HorizontalGroup("ImageColor")] [LabelWidth(75)] Image ImgColor;
    [SerializeField] [HorizontalGroup("ImageColor")] [LabelWidth(75)] Color onImgColor;
    [SerializeField] [HorizontalGroup("ImageColor")] [LabelWidth(75)] Color offImgColor;

    [SerializeField] [HorizontalGroup("TextColor")] [LabelWidth(75)] Text TxtColor;
    [SerializeField] [HorizontalGroup("TextColor")] [LabelWidth(75)] Color onTxtColor;
    [SerializeField] [HorizontalGroup("TextColor")] [LabelWidth(75)] Color offTxtColor;
    [Space]
    [SerializeField] ButtonToggleGroup group;

    public UnityEvent<bool> onClick;
    private void Awake()
    {
        GetComponent<Button>().onClick.AddListener(OnClick);
        group?.Add(this);
    }
    public bool IsOn
    {
        get
        {
            return isOn;
        }
        set
        {
            if (isOn != value)
            {
                GetComponent<Button>().onClick.Invoke();
            }
        }
    }
    /// <summary>
    /// 通告开关
    /// </summary>
    /// <param name="isOn">开启或关闭</param>
    /// <param name="sendCallback">是否触发事件</param>
    public void NotifyToggleOn(bool isOn, bool sendCallback)
    {
        this.isOn = isOn;
        if (sendCallback)
        {
            onClick.Invoke(isOn);
        }
        ChangeSprite();
    }
    private void OnClick()
    {
        isOn = !isOn;
        onClick.Invoke(isOn);
        ChangeSprite();
        group?.ChangeToggle(this);
    }
    [Button("更新状态")]
    private void ChangeSprite()
    {
        if (Img)
        {
            Img.sprite = isOn ? onSprite : offSprite;
        }
        if (Txt)
        {
            Txt.text = isOn ? onText : offText;
        }
        if (ImgColor)
        {
            ImgColor.color = isOn ? onImgColor : offImgColor;
        }
        if (TxtColor)
        {
            TxtColor.color = isOn ? onTxtColor : offTxtColor;
        }
    }
    private void OnDestroy()
    {
        group?.Sub(this);
    }
}
